#include "UILabel.h"
#include "UIControlDes.h"
#include "UIControlThemeInterpretor.h"
#include <aknutils.h>
#include "TextConverter.h"

UILabel::UILabel() : iLabelText(NULL),
					iStringSplitter(NULL)
{
}

UILabel::~UILabel()
{
	delete iLabelText;
	iLabelText = NULL;
	
	delete iStringSplitter;
	iStringSplitter = NULL;
}

void UILabel::Draw(CGraphicsContext& aGc)
	{
	//draw border
	UIControlThemeInterpretor::DrawBorder(aGc,this);
	
	//draw Text
	if(iStringSplitter)
		iStringSplitter->Draw(aGc);
	}

void UILabel::Layout(TRect rc)
	{
	if(iStringSplitter)
		iStringSplitter->Layout(rc);
		
	UIControl::Layout(rc);
	}

TSize UILabel::GetControlSizeL()
	{
	
	if(!iLabelText)
		return TSize(0,0);
	
	if(!iStringSplitter)
		iStringSplitter = CStringSplitter::NewL(*iLabelText);
	
	
	iStringSplitter->ParseL();
	TInt width = iRect.Width();
	if(iRect.Width() == 0 && iStyle.iWidth.GetUnit() != UILength::ELU_Pixel)
		width = 9999;
		
	iStringSplitter->SplitTextBlock(width);
	return iStringSplitter->EstimateSize();
	
//	return iStringSplitter->GetContentSize();
	
	}

//TSize UILabel::GetMinimumSize()
//	{
//	return TSize(20,10);
//	}

void UILabel::GetTagName(TDes8& aDes)
	{
	aDes.Copy(KCtrlLabel);
	}

void UILabel::SetTextL(const TDesC& aDes)
	{
	iLabelText = HBufC::NewL(aDes.Length());
	CleanupStack::PushL(iLabelText);
	
	TPtr ptr = iLabelText->Des();
	ptr.Copy(aDes);
	
	CleanupStack::Pop(iLabelText);
	}

void UILabel::SetPropertyL(const TDesC& aProperty,const TDesC& aValue)
	{
	if(aProperty.Compare(KPropertyText) == 0)
		{
		HBufC* bufVal = HBufC::NewL(aValue.Length());
		CleanupStack::PushL(bufVal);
		TPtr ptr = bufVal->Des();
		ptr.Copy(aValue);
		
		AknTextUtils::ReplaceCharacters(ptr,_L("{"),'<');
		AknTextUtils::ReplaceCharacters(ptr,_L("}"),'>');
				
		SetTextL(ptr);
		
		CleanupStack::PopAndDestroy(1);
		}
	else
		UIControl::SetPropertyL(aProperty,aValue);
	}
